package Java攻略._5_流式操作_lambda表达式与方法_引用的相关问题._4_Map接口的默认方法;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class _10_仅更新特定单词的出险次数 {

    public Map<String,Integer> countWords(String passage, String... strings){
        Map<String,Integer> wordCounts = new HashMap<>();
        Arrays.stream(strings)
                .forEach(s -> wordCounts.put(s,0));

        Arrays.stream(passage.split(" "))
                .forEach(word -> wordCounts.computeIfPresent(
                                                        word,
                                                        (key,val) -> val + 1
                        ));
        return wordCounts;
    }
}
